/**
 * @note 外链验证
 * @param {String} path
 * @return {Boolean}
 */
export function isExternal(path) {
    return /^(https?:|mailto:|tel:)/.test(path);
}

/**
 * @note 路由鉴权
 * @param {Array} roles - [ 角色数组 ]
 * @param {Object} route - [ 路由对象 ]
 */
function hasPermission(route, roles) {
    if (route.meta && route.meta.roles) {
        return roles.some(role => route.meta.roles.includes(role));
    } else {
        return true;
    }
}

/**
 * @note 递归处理
 * @param {Array} routes - [ 路由数组 ]
 * @param {Array} roles - [ 对象数组 ]
 * @return { Array } res - [ 过滤后的数组 ]
 */
export function filterAsyncRoutes(routes, roles) {
    const res = [];
    routes.forEach(route => {
        // 进行浅拷贝，存在递归时需要对  children 进行重写
        let tmp = { ...route };
        if (hasPermission(tmp, roles)) {
            if (tmp.children) {
                tmp.children = filterAsyncRoutes(tmp.children, roles);
            }
            res.push(tmp);
        }
    });
    return res;
}